package com.anolesoft.yr.mbus.crm.mapper;

import com.anolesoft.yr.mbus.crm.entity.CrmBaseDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;
import org.apache.ibatis.type.LocalDateTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/9 16:00
 */
@Mapper
public interface CrmBaseMapper {
    @Insert("insert into crm_base (" +
            "objuid," +
            "fac_code," +
            "base_region_uid," +
            "crm_code," +
            "crm_name," +
            "crm_other_name," +
            "crm_address," +
            "establish_time," +
            "crm_website," +
            "email," +
            "contact_person," +
            "contact_number," +
            "fax_number," +
            "crm_grade," +
            "legal_person," +
            "company_type," +
            "bank," +
            "account," +
            "tax_number," +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{baseRegionUid,jdbcType=VARCHAR}," +
            "#{crmCode,jdbcType=VARCHAR}," +
            "#{crmName,jdbcType=VARCHAR}," +
            "#{crmOtherName,jdbcType=VARCHAR}," +
            "#{crmAddress,jdbcType=VARCHAR}," +
            "#{establishTime,jdbcType=DATE}," +
            "#{crmWebsite,jdbcType=VARCHAR}," +
            "#{email,jdbcType=VARCHAR}," +
            "#{contactPerson,jdbcType=VARCHAR}," +
            "#{contactNumber,jdbcType=VARCHAR}," +
            "#{faxNumber,jdbcType=VARCHAR}," +
            "#{crmGrade,jdbcType=VARCHAR}," +
            "#{legalPerson,jdbcType=VARCHAR}," +
            "#{companyType,jdbcType=VARCHAR}," +
            "#{bank,jdbcType=VARCHAR}," +
            "#{account,jdbcType=VARCHAR}," +
            "#{taxNumber,jdbcType=VARCHAR}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(CrmBaseDO crmBaseDO);

    @Update("update crm_base set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "base_region_uid = #{baseRegionUid,jdbcType=VARCHAR}," +
            "crm_code = #{crmCode,jdbcType=VARCHAR}," +
            "crm_name = #{crmName,jdbcType=VARCHAR}," +
            "crm_other_name = #{crmOtherName,jdbcType=VARCHAR}," +
            "crm_address = #{crmAddress,jdbcType=VARCHAR}," +
            "establish_time = #{establishTime,jdbcType=DATE}," +
            "crm_website = #{crmWebsite,jdbcType=VARCHAR}," +
            "email = #{email,jdbcType=VARCHAR}," +
            "contact_person = #{contactPerson,jdbcType=VARCHAR}," +
            "contact_number = #{contactNumber,jdbcType=VARCHAR}," +
            "fax_number = #{faxNumber,jdbcType=VARCHAR}," +
            "crm_grade = #{crmGrade,jdbcType=VARCHAR}," +
            "legal_person = #{legalPerson,jdbcType=VARCHAR}," +
            "company_type = #{companyType,jdbcType=VARCHAR}," +
            "bank = #{bank,jdbcType=VARCHAR}," +
            "account = #{account,jdbcType=VARCHAR}," +
            "tax_number = #{taxNumber,jdbcType=VARCHAR}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR}" +
            " where objuid = #{objuid,jdbcType=VARCHAR}")
    int update(CrmBaseDO crmBaseDO);

    @Update("update crm_base set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "CrmBaseResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "base_region_uid", property = "baseRegionUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "crm_code", property = "crmCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "crm_name", property = "crmName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "crm_other_name", property = "crmOtherName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "crm_address", property = "crmAddress", jdbcType = JdbcType.VARCHAR),
            @Result(column = "establish_time", property = "establishTime", typeHandler = LocalDateTypeHandler.class),
            @Result(column = "crm_website", property = "crmWebsite", jdbcType = JdbcType.VARCHAR),
            @Result(column = "email", property = "email", jdbcType = JdbcType.VARCHAR),
            @Result(column = "contact_person", property = "contactPerson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "contact_number", property = "contactNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fax_number", property = "faxNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "crm_grade", property = "crmGrade", jdbcType = JdbcType.VARCHAR),
            @Result(column = "legal_person", property = "legalPerson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_type", property = "companyType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "bank", property = "bank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "account", property = "account", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tax_number", property = "taxNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "base_region_uid", property = "region", one = @One(select = "com.anolesoft.yr.mbus.base.mapper.BaseRegionMapper.selectOneById", fetchType = FetchType.EAGER)),
            @Result(column = "objuid", property = "contacts", many = @Many(select = "com.anolesoft.yr.mbus.base.mapper.BaseContactMapper.selectListByTargetUid", fetchType = FetchType.DEFAULT))

    })
    @Select("select " +
            "objuid, fac_code, base_region_uid, crm_code, crm_name," +
            "crm_other_name, crm_address, establish_time, crm_website," +
            "email, contact_person, contact_number, fax_number, crm_grade," +
            "legal_person, company_type, bank, account, tax_number," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from crm_base where del_flag =0 ")
    List<CrmBaseDO> selectAll();

    @ResultMap("CrmBaseResultMap")
    @Select("select " +
            "objuid, fac_code, base_region_uid, crm_code, crm_name," +
            "crm_other_name, crm_address, establish_time, crm_website," +
            "email, contact_person, contact_number, fax_number, crm_grade," +
            "legal_person, company_type, bank, account, tax_number," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from crm_base where del_flag =0 ")
    List<CrmBaseDO> selectPageAll(Page<CrmBaseDO> page);

    @ResultMap("CrmBaseResultMap")
    @Select("select " +
            "objuid, fac_code, base_region_uid, crm_code, crm_name," +
            "crm_other_name, crm_address, establish_time, crm_website," +
            "email, contact_person, contact_number, fax_number, crm_grade," +
            "legal_person, company_type, bank, account, tax_number," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from crm_base where del_flag =0 and objuid = #{id,jdbcType=VARCHAR}")
    CrmBaseDO selectOneById(@Param("id") String id);

    @ResultMap("CrmBaseResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, base_region_uid, crm_code, crm_name," +
            "crm_other_name, crm_address, establish_time, crm_website," +
            "email, contact_person, contact_number, fax_number, crm_grade," +
            "legal_person, company_type, bank, account, tax_number," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from crm_base where del_flag =0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.baseRegionUid !=null and o.baseRegionUid != '' \">" +
            "   and base_region_uid = #{o.baseRegionUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmCode !=null and o.crmCode != '' \">" +
            "   and crm_code like #{o.crmCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmName !=null and o.crmName != '' \">" +
            "   and crm_name like #{o.crmName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmOtherName !=null and o.crmOtherName != '' \">" +
            "   and crm_other_name like #{o.crmOtherName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmAddress !=null and o.crmAddress != '' \">" +
            "   and crm_address like #{o.crmAddress, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<CrmBaseDO> selectList(@Param("o") CrmBaseDO crmBaseDO);

    @ResultMap("CrmBaseResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, base_region_uid, crm_code, crm_name," +
            "crm_other_name, crm_address, establish_time, crm_website," +
            "email, contact_person, contact_number, fax_number, crm_grade," +
            "legal_person, company_type, bank, account, tax_number," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from crm_base where del_flag =0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.baseRegionUid !=null and o.baseRegionUid != '' \">" +
            "   and base_region_uid = #{o.baseRegionUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmCode !=null and o.crmCode != '' \">" +
            "   and crm_code like #{o.crmCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmName !=null and o.crmName != '' \">" +
            "   and crm_name like #{o.crmName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmOtherName !=null and o.crmOtherName != '' \">" +
            "   and crm_other_name like #{o.crmOtherName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmAddress !=null and o.crmAddress != '' \">" +
            "   and crm_address like #{o.crmAddress, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<CrmBaseDO> selectPageList(@Param("o") CrmBaseDO crmBaseDO, Page<CrmBaseDO> page);

    @ResultMap("CrmBaseResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, base_region_uid, crm_code, crm_name," +
            "crm_other_name, crm_address, establish_time, crm_website," +
            "email, contact_person, contact_number, fax_number, crm_grade," +
            "legal_person, company_type, bank, account, tax_number," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from crm_base where del_flag =0 " +
//            "<if test =\"o.crmCode !=null and o.crmCode != '' \">" +
//            "   and crm_code like concat(#{o.crmCode, jdbcType=VARCHAR},'%') " +
//            "</if>" +
//            "<if test =\"o.crmName !=null and o.crmName != '' \">" +
//            "   and crm_name like concat(#{o.crmName, jdbcType=VARCHAR},'%') " +
//            "</if>" +
//            "<if test =\"o.crmOtherName !=null and o.crmOtherName != '' \">" +
//            "   and crm_other_name like concat(#{o.crmOtherName, jdbcType=VARCHAR},'%') " +
//            "</if>" +
            "<if test=\"o.crmCode!=null or o.crmName!=null or o.crmOtherName!=null\">" +
                "and (" +
                    "<trim prefixOverrides=\"or\">" +
                        "<if test =\"o.crmCode !=null and o.crmCode != '' \">" +
                        "  or crm_code like concat(#{o.crmCode, jdbcType=VARCHAR},'%') " +
                        "</if>" +
                        "<if test =\"o.crmName !=null and o.crmName != '' \">" +
                        "  or crm_name like concat(#{o.crmName, jdbcType=VARCHAR},'%') " +
                        "</if>" +
                        "<if test =\"o.crmOtherName !=null and o.crmOtherName != '' \">" +
                        "  or crm_other_name like concat(#{o.crmOtherName, jdbcType=VARCHAR},'%') " +
                        "</if>" +
                    "</trim>" +
                ")" +
            "</if>" +
            "</script>")
    List<CrmBaseDO> selectComboxList(@Param("o") CrmBaseDO crmBaseDO, Page<CrmBaseDO> page);

    @Select("select count(objuid) from crm_base where del_flag = 0 and crm_code = #{crmCode, jdbcType=VARCHAR}")
    Integer checkUniqueForCrmCode(@Param("crmCode") String crmCode);

    @Select("select count(objuid) from crm_base where del_flag = 0 and crm_name = #{crmName, jdbcType=VARCHAR}")
    Integer checkUniqueForCrmName(@Param("crmName") String crmName);

    @Select("select count(objuid) from crm_base where del_flag = 0 and crm_code = #{crmCode, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForCrmCodeUpdate(@Param("crmCode") String companyCode, @Param("id") String id);

    @Select("select count(objuid) from crm_base where del_flag = 0 and crm_name = #{crmName, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForCrmNameUpdate(@Param("crmName") String crmName, @Param("id") String id);
}
